package com.hsq.androidproject.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Administrator on 2016/12/21.
 */

public class HttpUtils {
    public static byte[] getData(String path) {
        ByteArrayOutputStream baos = null;
        try {
            URL url = new URL(path);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            int responseCode = connection.getResponseCode();
            if (responseCode == 200) {
                baos = new ByteArrayOutputStream();
                InputStream inputStream = connection.getInputStream();
                int length = -1;
                byte[] bytes = new byte[1024 * 4];
                while ((length = inputStream.read(bytes)) != -1) {
                    baos.write(bytes, 0, length);
                }
                byte[] data = baos.toByteArray();
                return data;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
